package org.example.service;

import org.example.dao.EmployeeDAO;
import org.example.dao.SalaryDAO;
import org.example.entity.Salary;
import org.example.utils.TimeUtils;

import java.math.BigDecimal;
import java.util.*;

public class SalaryService {
    public static void addSalary(Double saBase,Double saPerformance,Double saInsurance,String empId,String saDate) {

        try {
            BigDecimal bigDecimal = new BigDecimal(saBase + saPerformance - saInsurance);
            double saActual = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();


            //dao新增薪资
            Salary salary = new Salary(
                    UUID.randomUUID().toString(),
                    empId,
                    saDate,
                    saBase,
                    saPerformance,
                    saInsurance,
                    saActual,
                    TimeUtils.getTime(),
                    TimeUtils.getTime());
            if (SalaryDAO.insertSalary(salary)) {
                System.out.println("========薪资添加成功=======");
                System.out.println("=========================");
            } else {
                System.out.println("========薪资添加失败=======");
                System.out.println("=========================");
            }


        } catch (Exception e) {
            System.out.println("数据处理错误！请重新输入");

        }


    }

    public static void updateSalary(Double newSaBase,Double newSaPerformance,Double newSaInsurance,String empId,String currentDate) {

        double newSaActual = newSaBase + newSaPerformance - newSaInsurance;

        //dao执行修改
        if (SalaryDAO.updateSalary(newSaBase, newSaPerformance, newSaInsurance, empId, currentDate,newSaActual)) {
            System.out.println("========薪资修改成功=======");
            System.out.println("=========================");
        } else {
            System.out.println("========薪资修改失败=======");
            System.out.println("=========================");
        }
    }

    public static void deleteSalary(String empId,String currentMonth) {
        if (SalaryDAO.deleteSalary(empId, currentMonth)) {
            System.out.println("========员工当月薪资数据删除成功！=========");
        } else {
            System.out.println("========员工当月薪资数据删除失败！=========");
        }

    }

    public static ArrayList<String[]> querySalaryByEmpId(String empId) {
        return SalaryDAO.querySalaryListByEmpId(empId);
    }

    public static ArrayList<String[]> querySalaryBySaDate(String saDate) {
        //dao根据年月时间查询所有历史薪资
        return SalaryDAO.querySalaryListBySaDate(saDate);
    }



    //检测员工数据是否存在
    public static boolean checkEmpEmpty(){
        ArrayList<String[]> data = EmployeeDAO.getEmployeeInfo();
        return data != null && data.isEmpty();
    }


    //序号范围校验
    public static boolean checkNumIsLegal(int num,ArrayList<String[]> data){
        return data == null || (num <= data.size() && num >= 1);
    }

    public static String[] getOldEmpSalaryInfo(ArrayList<String[]> data,Integer num){
        //已选择员工薪资数据回显
        double saBase = 0;
        double saPerformance = 0;
        double saInsurance = 0;
        String empId = "";
        String currentDate = TimeUtils.getSalaryDate();
        if (data != null) {
            empId = data.get(num - 1)[0];
            ArrayList<Double> empSalary = SalaryDAO.querySalaryByEmpId(empId, currentDate);
            if (empSalary != null && !empSalary.isEmpty()) {
                saBase = empSalary.get(0);
                saPerformance = empSalary.get(1);
                saInsurance = empSalary.get(2);
            }
        }
        return new String[]{empId, String.valueOf(saBase), String.valueOf(saPerformance), String.valueOf(saInsurance),currentDate};

    }

    public static boolean deleteSalaryById(String id) {
        return SalaryDAO.deleteSalaryById(id);
    }
}
